package com.testfile.binfo.uitl;

import com.testfile.binfo.geteway.FileController;
import com.testfile.binfo.model.FileInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 */
public class FileUtil {
    //标识当前类的日志信息
    private static Logger LOGGER= LoggerFactory.getLogger(FileController.class);

    /**
     * 查询路径下所有文件
     * @param filePath
     * @return
     */
    public static List<FileInfo> getFiles(String filePath){
        List<FileInfo> fileInfos=new ArrayList<FileInfo>();
        fileInfos.clear();
        File file=new File(filePath);
        File []files=file.listFiles();
        for(File f:files){
            FileInfo fileInfo=new FileInfo();
            fileInfo.setFileName(f.getName());
            fileInfo.setFilePath(f.getPath());
            fileInfos.add(fileInfo);
        }
        return fileInfos;
    }

    /**
     * 文件下载
     * @param response
     * @param filePathName
     */
    public static void downloadFile(HttpServletResponse response,String filePathName){
        //截取文件名
        String fileName=filePathName.substring(filePathName.lastIndexOf("/")+1);
        File file = new File(filePathName);
        if (file.exists()) {
            response.reset();
            response.setHeader("content-type", "application/octet-stream");
            response.setContentType("application/octet-stream");
            try {
                response.setHeader("Content-Disposition", "attachment;filename="+new String(fileName.getBytes("utf-8"),"ISO-8859-1"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            byte[] buffer = new byte[1024];
            FileInputStream fis = null;
            BufferedInputStream bis = null;
            try {
                fis = new FileInputStream(file);
                bis = new BufferedInputStream(fis);
                OutputStream os = response.getOutputStream();
                int i = bis.read(buffer);
                while (i != -1) {
                    os.write(buffer, 0, i);
                    i = bis.read(buffer);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (bis != null) {
                    try {
                        bis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (fis != null) {
                    try {
                        fis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    /**
     * 文件上传
     * @param file
     * @param directPath
     * @return
     */
    public static String fileUpload(MultipartFile file,String directPath){
        System.out.println(directPath);
        //文件内容为空也可以上传
        if(null==file){
            return "上传失败,请选择文件";
        }else{
            File file1=new File(directPath);
            if(!file1.isDirectory()){
                file1.mkdir();
            }
            //获取上传时的文件名
            String fileName=file.getOriginalFilename();
            if(!directPath.endsWith("/")){
                directPath+="/";
            }
            System.out.println(directPath+fileName);
            File destFile=new File(directPath+fileName);
            try {
                //把文件内容写入目标磁盘中
                file.transferTo(destFile);
                //日志提示
                LOGGER.info("上传成功");
                return "上传成功";
            }catch (IOException e){
                e.printStackTrace();
                LOGGER.error(e.toString(),e);
                return "上传失败";
            }
        }
    }
}
